package tq.com.tqteacher.activity.newPublish;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.githang.statusbar.StatusBarCompat;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQTWDYPublishModel;
import tq.com.tqteacher.view.TQHeader3ViewHold;
import tq.com.tqteacher.view.TQQNoManagerView;

public class TQTWDYSetQNoActivity extends BaseActivity {
    private TQHeader5View mHeader5View;
    private TQQNoManagerView mTQQNoManagerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBar();
        setContentView(R.layout.activity_setqno_layout);
        mTQQNoManagerView = findViewById(R.id.region);

        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTQQNoManagerView.addRegion();
            }
        });

        findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TQPublishType.showPublishNextStep(TQTWDYSetQNoActivity.this);
                gotoNext();
            }
        });

        mHeader5View = findViewById(R.id.title);
        mHeader5View.getTitleView().setText("提问&群答疑");
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQTWDYSetQNoActivity.this.finish();
            }
        });

        if(!(TQPublishType.getSharePublishItem() instanceof TQTWDYPublishModel)){
            return;
        }
        if(TQPublishType.getSharePublishItem().isModify()){
            TQTWDYPublishModel lModel = (TQTWDYPublishModel) TQPublishType.getSharePublishItem();
            mTQQNoManagerView.setData(lModel.getRegionData());
        }
    }

    private void gotoNext(){
        if(!mTQQNoManagerView.checkDataValid()){
            AlertDialogHelper.show(this, "输入题号不合法");
            return;
        }
        ArrayList<TQQNoManagerView.TQRegionData> lRegionData = mTQQNoManagerView.getData();
        if(lRegionData.size() == 0){
            AlertDialogHelper.show(this, "分享题号不能为空");
            return;
        }

        if(!(TQPublishType.getSharePublishItem() instanceof TQTWDYPublishModel)){
            return;
        }

        TQTWDYPublishModel lPublish = (TQTWDYPublishModel)TQPublishType.getSharePublishItem();
        lPublish.setRegionData(lRegionData);

        if(lPublish.isModify()){
            lPublish.onMoifyQuestion(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQTWDYSetQNoActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQTWDYSetQNoActivity.this.finish();
                }
            });
        }else{
            lPublish.onPulishComplete(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQTWDYSetQNoActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQPublishType.getSharePublishItem().onNewPublishResult(pNetResult.getData());

                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.add, null));
                    TQPublishType.showPublishNextStep(TQTWDYSetQNoActivity.this);
                    gotoResult();
                }
            });
        }

    }

    private void gotoResult(){
        Intent lIntent = new Intent(this, TQTWDYResultActivity.class);
        startActivity(lIntent);
    }
}
